package cn.ht.jywrwater.module.ht.controller.admin.watercount;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.ht.jywrwater.framework.common.pojo.PageResult;
import cn.ht.jywrwater.framework.common.pojo.CommonResult;
import static cn.ht.jywrwater.framework.common.pojo.CommonResult.success;

import cn.ht.jywrwater.framework.excel.core.util.ExcelUtils;

import cn.ht.jywrwater.framework.operatelog.core.annotations.OperateLog;
import static cn.ht.jywrwater.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.ht.jywrwater.module.ht.controller.admin.watercount.vo.*;
import cn.ht.jywrwater.module.ht.dal.dataobject.watercount.WaterCountDO;
import cn.ht.jywrwater.module.ht.convert.watercount.WaterCountConvert;
import cn.ht.jywrwater.module.ht.service.watercount.WaterCountService;

@Tag(name = "管理后台 - 水量统计")
@RestController
@RequestMapping("/ht/water-count")
@Validated
public class WaterCountController {

    @Resource
    private WaterCountService waterCountService;

    @PostMapping("/create")
    @Operation(summary = "创建水量统计")
    @PreAuthorize("@ss.hasPermission('ht:water-count:create')")
    public CommonResult<Long> createWaterCount(@Valid @RequestBody WaterCountCreateReqVO createReqVO) {
        return success(waterCountService.createWaterCount(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新水量统计")
    @PreAuthorize("@ss.hasPermission('ht:water-count:update')")
    public CommonResult<Boolean> updateWaterCount(@Valid @RequestBody WaterCountUpdateReqVO updateReqVO) {
        waterCountService.updateWaterCount(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除水量统计")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ht:water-count:delete')")
    public CommonResult<Boolean> deleteWaterCount(@RequestParam("id") Long id) {
        waterCountService.deleteWaterCount(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得水量统计")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ht:water-count:query')")
    public CommonResult<WaterCountRespVO> getWaterCount(@RequestParam("id") Long id) {
        WaterCountDO waterCount = waterCountService.getWaterCount(id);
        return success(WaterCountConvert.INSTANCE.convert(waterCount));
    }

    @GetMapping("/list")
    @Operation(summary = "获得水量统计列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('ht:water-count:query')")
    public CommonResult<List<WaterCountRespVO>> getWaterCountList(@RequestParam("ids") Collection<Long> ids) {
        List<WaterCountDO> list = waterCountService.getWaterCountList(ids);
        return success(WaterCountConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得水量统计分页")
    @PreAuthorize("@ss.hasPermission('ht:water-count:query')")
    public CommonResult<PageResult<WaterCountRespVO>> getWaterCountPage(@Valid WaterCountPageReqVO pageVO) {
        PageResult<WaterCountDO> pageResult = waterCountService.getWaterCountPage(pageVO);
        return success(WaterCountConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出水量统计 Excel")
    @PreAuthorize("@ss.hasPermission('ht:water-count:export')")
    @OperateLog(type = EXPORT)
    public void exportWaterCountExcel(@Valid WaterCountExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<WaterCountDO> list = waterCountService.getWaterCountList(exportReqVO);
        // 导出 Excel
        List<WaterCountExcelVO> datas = WaterCountConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "水量统计.xls", "数据", WaterCountExcelVO.class, datas);
    }

}
